package com.mvvm.demo.di

import android.os.Build
import android.util.Log
import com.mvvm.base.util.prefs.SharedPreferencesManager
import com.mvvm.demo.BuildConfig
import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.logging.HttpLoggingInterceptor
import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.provider
import org.kodein.di.generic.singleton
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.IOException
import java.util.concurrent.TimeUnit
import javax.net.ssl.HostnameVerifier

private const val HTTP_CLIENT_MODULE_TAG = "httpClientModule"
const val HTTP_CLIENT_MODULE_INTERCEPTOR_LOG_TAG = "http"
const val TIME_OUT_SECONDS = 10
const val BASE_URL = "https://apigw.gialen.com/"

val httpClientModule = Kodein.Module(HTTP_CLIENT_MODULE_TAG) {

    bind<Retrofit.Builder>() with provider { Retrofit.Builder() }

    bind<OkHttpClient.Builder>() with provider { OkHttpClient.Builder() }

    bind<Retrofit>() with singleton {
        instance<Retrofit.Builder>()
                .baseUrl(BASE_URL)
                .client(instance())
                .addConverterFactory(GsonConverterFactory.create())
                .build()
    }

    bind<Interceptor>(HTTP_CLIENT_MODULE_INTERCEPTOR_LOG_TAG) with singleton {
        HttpLoggingInterceptor().apply {
            level = when (BuildConfig.DEBUG) {
                true -> HttpLoggingInterceptor.Level.BODY
                false -> HttpLoggingInterceptor.Level.NONE
            }
        }
    }



    bind<OkHttpClient>() with singleton {
        val builder = OkHttpClient.Builder()

        val sessionId: String = SharedPreferencesManager.Companion.instance!!.getString(SharedPreferencesManager.SPCommons.SESSIONID, "")!!
        builder.addInterceptor(object : Interceptor {
            @Throws(IOException::class)
            override fun intercept(chain: Interceptor.Chain): Response {
                val request = chain.request()
                        .newBuilder()
                        .addHeader("platfrom", "1")
                        .addHeader("terminal", "2")
                        .addHeader("sessionId", sessionId)
                        .addHeader("appVersion", BuildConfig.VERSION_NAME)
                        .addHeader("mobileModel", Build.BRAND + " " + Build.MODEL)
                        .addHeader("IMEI", Build.SERIAL)
                        .build()
                return chain.proceed(request)
            }
        })
        if (BuildConfig.DEBUG) {

            // Log信息拦截器
            val loggingInterceptor = HttpLoggingInterceptor(object : HttpLoggingInterceptor.Logger {
                override fun log(message: String) {
                    Log.d("http", message)
                }
            })
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
            //设置 Debug Log 模式
            builder.addInterceptor(loggingInterceptor)
        }
        //            HTTPSCerUtils.setTrustAllCertificate(builder);
//            builder.sslSocketFactory(createSSLSocketFactory());
        builder.hostnameVerifier(HostnameVerifier { hostname, session -> true })
        builder.connectTimeout(5, TimeUnit.SECONDS)
        builder.readTimeout(5, TimeUnit.SECONDS)
        builder.writeTimeout(5, TimeUnit.SECONDS)
        builder.build()

    }
}